﻿using CMS.Controls.TabPageTemplate;
using CMS.Infos;
using CMS.REST;
using System;
using System.Windows.Forms;

namespace CMS.Controls.DisplayTabPages.SettingTabPages
{
    public class RoomEditorAdaptor : EditorAdaptor
    {
        public override string ItemName => "课室";

        public override string PkField => "Name";

        public override string NameField => "Name";

        public override string API => "apis/v1/rooms";

        public override Type ElementType => typeof(Room);

        public override bool AllowIndexItem => Permission.CheckPermission(Permission.Room.Index);

        public override bool AllowCreateItem => Permission.CheckPermission(Permission.Room.Save);

        public override bool AllowUpdateItem => Permission.CheckPermission(Permission.Room.Update);


        public override bool AllowDeleteItem => false;

        private FormPanel FP_Main;
        private TextBox TB_Room;
        private ComboBox CB_Building;
        private MyNumericUpDown NUD_StudentCount;

        public override void InitializeEditor(Panel editorPanel, BindingSource source)
        {
            FP_Main = new FormPanel();
            _SettingFormPanel(editorPanel, FP_Main);
            FP_Main.SuspendLayout();
            TB_Room = new MyTextBox();
            TB_Room.DataBindings.Add("Text", source, "Name");

            FP_Main.AddField( "课室名", TB_Room, control => control.Text.Length >= 3);

            CB_Building = new MyComboBox();
            CB_Building.DataBindings.Add("Text", source, "Building");
            CB_Building.AutoCompleteSource = AutoCompleteSource.ListItems;
            CB_Building.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            RESTResponse response = RESTClient.HttpGetManaged("apis/v1/building_names");
            if (response.IsSuccess)
            {
                CB_Building.DataSource = response.Data.ToObject<string[]>();
            }
            FP_Main.AddField("所属教学楼", CB_Building,control=>!string.IsNullOrWhiteSpace(control.Text));


            NUD_StudentCount = new MyNumericUpDown();
            NUD_StudentCount.DataBindings.Add("Value", source, "SeatCount");
            NUD_StudentCount.Maximum = 1000;
            FP_Main.AddField( "学生人数", NUD_StudentCount);
            FP_Main.ResumeLayout();
        }

        public override bool UpdateDisplayMode(Panel editorPanel, DisplayMode mode)
        {
            switch (mode)
            {
                case DisplayMode.New:
                    FP_Main.ReadOnly = false;
                    TB_Room.Enabled = CB_Building.Enabled = NUD_StudentCount.Enabled = true;
                    break;
                case DisplayMode.Edit:
                    FP_Main.ReadOnly = false;
                    TB_Room.Enabled = false;
                    CB_Building.Enabled = NUD_StudentCount.Enabled = true;
                    break;
                case DisplayMode.Preview:
                    FP_Main.ReadOnly = true;
                    TB_Room.Enabled = CB_Building.Enabled = NUD_StudentCount.Enabled = false;
                    break;
                case DisplayMode.None:
                    FP_Main.ClearContent();
                    goto case DisplayMode.Preview;
            }
            return true;
        }
        public override bool ValidateForm()
        {
            return FP_Main.Validate();
        }
    }

}
